﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AlexandriaPressUserObject;
using AlexandriaPressUserBusinessLayer;

namespace AlexandriaPressWebsite
{
    public partial class MainTemplate : System.Web.UI.MasterPage
    {

        protected void Page_Load(object sender, EventArgs e)
        {

            if (Session["userID"] != null)
            {
                signUpButton.Visible = false;
                logInButton.Text = "Log Out";
            }

            if (Session["userType"] != null)
            {
                if (Convert.ToString(Session["userType"]) == "Type_Author")
                {
                    authorAppButton.Visible = false;
                    searchDraftsButton.Visible = true;
                    submitDraftButton.Visible = true;
                    searchBooksButton.Visible = true;
                    MySalesButton.Visible = true;
                }

                if (Convert.ToString(Session["userType"]) == "Type_ProofReader")
                {
                    authorAppButton.Visible = false;
                    proofreaderConsoleButton.Visible = true;
                    viewDraftsProofreaderButton.Visible = true;
                }

                if (Convert.ToString(Session["userType"]) == "Type_Admin")
                {
                    authorAppButton.Visible = false;
                    reportsButton.Visible = true;
                    manageAuthorsButton.Visible = true;
                    manageProofReadersButton.Visible = true;
                    manageUsersButton.Visible = true;
                    authorApplicationsAdminButton.Visible = true;
                }

                if (Convert.ToString(Session["userType"]) == "Type_Buyer")
                {
                    authorAppButton.Visible = true;
                    myAccountButton.Visible = true;
                    myTransactionsButton.Visible = true;
                }
            }
            else
            {
                    authorAppButton.Visible = false;
                    reportsButton.Visible = false;
                    manageAuthorsButton.Visible = false;
                    manageProofReadersButton.Visible = false;
                    manageUsersButton.Visible = false;
                    authorApplicationsAdminButton.Visible = false;
                    proofreaderConsoleButton.Visible = false;
                    viewDraftsProofreaderButton.Visible = false;
                    searchDraftsButton.Visible = false;
                    submitDraftButton.Visible = false;
                    searchBooksButton.Visible = false;
                    MySalesButton.Visible = false;
                    myAccountButton.Visible = false;
                    myTransactionsButton.Visible = false;
            }



            int numItems = Convert.ToInt32(Session["numItems"]);
            if (numItems == 0)
            {
                cartLink.Text = "Cart: 0 items";
            }
            if (numItems == 1)
            {
                cartLink.Text = "Cart: 1 item";
            }
            if (numItems > 1)
            {
                cartLink.Text = "Cart: " + Convert.ToString(numItems) + " items";
            }       
        }

           
        

   

        protected void TermsButton_Click(object sender, EventArgs e)
        {
            if (Session["newOrders"] != null)
            {
                Session.Remove("orders");
                Session.Add("orders", (List<OrderItems>)Session["newOrders"]);
            }
            Response.Redirect("~/MasterPages/TermsConditions.aspx");
        }

        protected void logInButton_Click(object sender, EventArgs e)
        {
            if (Session["userID"] != null)
            {
                Session.RemoveAll();
 
                Session.Clear();
                Session.Abandon();
                System.Web.Security.FormsAuthentication.SignOut();
                Response.Redirect("~/MasterPages/MainPage.aspx");
            }
            Response.Redirect("~/MasterPages/UserPages/LoginPage.aspx");
        }

        protected void myAccountButton_Click(object sender, EventArgs e)
        {
            if (Session["newOrders"] != null)
            {
                Session.Remove("orders");
                Session.Add("orders", (List<OrderItems>)Session["newOrders"]);
            }
            Response.Redirect("~/MasterPages/UserPages/UserProfilePage.aspx");
        }

        protected void myTransactionsButton_Click(object sender, EventArgs e)
        {
            if (Session["newOrders"] != null)
            {
                Session.Remove("orders");
                Session.Add("orders", (List<OrderItems>)Session["newOrders"]);
            }
            Response.Redirect("~/MasterPages/UserPages/UserTransactionsPage.aspx");
        }

        protected void mostPopularButton_Click(object sender, EventArgs e)
        {
            if (Session["newOrders"] != null)
            {
                Session.Remove("orders");
                Session.Add("orders", (List<OrderItems>)Session["newOrders"]);
            }
            Response.Redirect("~/MasterPages/UserPages/MostPopular.aspx");
        }

        protected void searchButton_Click(object sender, EventArgs e)
        {
            if (Session["newOrders"] != null)
            {
                Session.Remove("orders");
                Session.Add("orders", (List<OrderItems>)Session["newOrders"]);
            }
            Response.Redirect("~/MasterPages/UserPages/SearchPage.aspx");
        }

        protected void searchBooksButton_Click(object sender, EventArgs e)
        {
            if (Session["newOrders"] != null)
            {
                Session.Remove("orders");
                Session.Add("orders", (List<OrderItems>)Session["newOrders"]);
            }
            Response.Redirect("~/MasterPages/AuthorPages/SearchBooks.aspx");
        }

        protected void authorAppButton_Click(object sender, EventArgs e)
        {
            if (Session["newOrders"] != null)
            {
                Session.Remove("orders");
                Session.Add("orders", (List<OrderItems>)Session["newOrders"]);
            }
            Response.Redirect("~/MasterPages/UserPages/AuthorApplicationPage.aspx");
        }

        protected void signUpButton_Click(object sender, EventArgs e)
        {
            if (Session["newOrders"] != null)
            {
                Session.Remove("orders");
                Session.Add("orders", (List<OrderItems>)Session["newOrders"]);
            }
            Response.Redirect("~/MasterPages/UserPages/CreateNewUser.aspx");
        }

        protected void reportsButton_Click(object sender, EventArgs e)
        {
            if (Session["newOrders"] != null)
            {
                Session.Remove("orders");
                Session.Add("orders", (List<OrderItems>)Session["newOrders"]);
            }
            Response.Redirect("~/MasterPages/AdminPages/SalesReport.aspx");
        }

        protected void draftsButton_Click(object sender, EventArgs e)
        {
            if (Session["newOrders"] != null)
            {
                Session.Remove("orders");
                Session.Add("orders", (List<OrderItems>)Session["newOrders"]);
            }           
        }

        protected void manageUsersButton_Click(object sender, EventArgs e)
        {
            if (Session["newOrders"] != null)
            {
                Session.Remove("orders");
                Session.Add("orders", (List<OrderItems>)Session["newOrders"]);
            }
            Response.Redirect("~/MasterPages/AdminPages/UserList.aspx");
        }

        protected void manageAuthorsButton_Click(object sender, EventArgs e)
        {
            if (Session["newOrders"] != null)
            {
                Session.Remove("orders");
                Session.Add("orders", (List<OrderItems>)Session["newOrders"]);
            }
            Response.Redirect("~/MasterPages/AdminPages/AuthorList.aspx");
        }

        protected void manageProofReadersButton_Click(object sender, EventArgs e)
        {
            if (Session["newOrders"] != null)
            {
                Session.Remove("orders");
                Session.Add("orders", (List<OrderItems>)Session["newOrders"]);
            }
            Response.Redirect("~/MasterPages/AdminPages/ProofreaderList.aspx");
        }

        protected void searchDraftsButton_Click(object sender, EventArgs e)
        {
            if (Session["newOrders"] != null)
            {
                Session.Remove("orders");
                Session.Add("orders", (List<OrderItems>)Session["newOrders"]);
            }
            Response.Redirect("~/MasterPages/AuthorPages/SearchAllDrafts.aspx");
        }

        protected void submitDraftButton_Click(object sender, EventArgs e)
        {
            if (Session["newOrders"] != null)
            {
                Session.Remove("orders");
                Session.Add("orders", (List<OrderItems>)Session["newOrders"]);
            }
            Response.Redirect("~/MasterPages/AuthorPages/SubmitDraft.aspx");
        }

        protected void authorApplicationsAdminButton_Click(object sender, EventArgs e)
        {
            if (Session["newOrders"] != null)
            {
                Session.Remove("orders");
                Session.Add("orders", (List<OrderItems>)Session["newOrders"]);
            }
            Response.Redirect("~/MasterPages/AdminPages/AuthorApplication.aspx");
        }

        protected void viewDraftsProofreaderButton_Click(object sender, EventArgs e)
        {
            if (Session["newOrders"] != null)
            {
                Session.Remove("orders");
                Session.Add("orders", (List<OrderItems>)Session["newOrders"]);
            }
            Response.Redirect("~/MasterPages/ProofreaderPages/AllDraftsForReview.aspx");
        }

        protected void proofreaderConsoleButton_Click(object sender, EventArgs e)
        {
            if (Session["newOrders"] != null)
            {
                Session.Remove("orders");
                Session.Add("orders", (List<OrderItems>)Session["newOrders"]);
            }
            Response.Redirect("~/MasterPages/ProofreaderPages/ProofReaderConsole.aspx");
        }

        protected void MySalesButton_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/MasterPages/AuthorPages/GetOverAllSale.aspx");
        }
    }
}
